package com.anpy.bill.service.impl;

import com.anpy.bill.entity.SecondClassifyEntity;
import com.anpy.bill.mapper.BillSecondClassifyMapper;
import com.anpy.bill.service.BillSecondClassifyService;
import com.anpy.common.service.BaseServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("billSecondClassifyServiceImpl")
public class BillSecondClassifyServiceImpl extends BaseServiceImpl<BillSecondClassifyMapper, SecondClassifyEntity> implements BillSecondClassifyService {

    @Override
    public boolean saveSecondClassify(String classifyName, Integer classifyId) {
        LambdaQueryWrapper<SecondClassifyEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondClassifyEntity::getName, classifyName);
        wrapper.eq(SecondClassifyEntity::getClassifyId, classifyId);
        SecondClassifyEntity entity = this.getOne(wrapper);
        if (entity == null) {
            entity = new SecondClassifyEntity();
            entity.setName(classifyName);
            entity.setClassifyId(classifyId);
            return this.save(entity);
        }
        return false;
    }

    @Override
    public List<SecondClassifyEntity> secondClassifyList(Integer classifyId, Integer userId) {
        LambdaQueryWrapper<SecondClassifyEntity> wrapper = new LambdaQueryWrapper<>();
        if (classifyId != -1) {
            wrapper.eq(SecondClassifyEntity::getClassifyId, classifyId);
        }
        wrapper.in(SecondClassifyEntity::getUserId, userId, -1);
        return this.list(wrapper);
    }

}
